package com.gua.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gua.common.request.BaseController;
import com.gua.common.response.AjaxResult;
import com.gua.user.response.DesktopMenuDTO;
import com.gua.user.response.ManagementMenuRespVO;
import com.gua.user.response.MenuDTO;
import com.gua.user.service.IDesktopMenuService;
import com.gua.user.service.IManagementMenuService;
import com.gua.user.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 */
@Slf4j
@Api(value = "菜单权限", tags = {"菜单权限"})
@RestController
public class MenuController extends BaseController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IDesktopMenuService desktopMenuService;

    @Autowired
    private IManagementMenuService managementMenuService;

//    @ApiOperation(value = "用户的菜单权限列表")
//    @GetMapping("/menu/list")
//    public AjaxResult<List<MenuDTO>> getMenusByUserId() {
//        List<MenuDTO> list = menuService.getMenusByUserId();
//        if (CollectionUtils.isEmpty(list)) {
//            list = new ArrayList<>();
//        }
//        return success(list);
//    }

    @ApiOperation(value = "用户的菜单权限列表")
    @GetMapping("/permission/getDesktopMenu")
    public AjaxResult<List<DesktopMenuDTO>> getDesktopMenu() {
        List<DesktopMenuDTO> menus = desktopMenuService.getDesktopMenu();
        return success(menus);
    }

    @ApiOperation(value = "用户的菜单权限列表")
    @GetMapping("/user/getManagementMenu")
    public AjaxResult<List<ManagementMenuRespVO>> getManagementMenu() {
        List<ManagementMenuRespVO> managementMenuRespVOs = managementMenuService.getMenusByUserId();
        return new AjaxResult<>(managementMenuRespVOs);
    }

//    @ApiOperation(value = "用户的菜单权限列表")
//    @GetMapping("/user/getManagementMenu")
//    public AjaxResult<JSONArray> getManagementMenu() {
//        StringBuffer sbuffer = new StringBuffer();
//        try {
//            //FileInputStream in = new FileInputStream(new File("E:\\electron_ws\\gua-tools\\server\\gua-tools-parent\\gua-tools-user\\src\\main\\resources\\menu.js"));
//            FileInputStream in = new FileInputStream(new File("C:\\Users\\2230\\Desktop\\electron_ws\\gua-tools\\server\\gua-tools-parent\\gua-tools-user\\src\\main\\resources\\menu.js"));
//            InputStreamReader inr = new InputStreamReader(in);
//            BufferedReader br = new BufferedReader(inr);//构造一个BufferedReader类来读取文件
//            String s = null;
//            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
//                sbuffer.append(System.lineSeparator() + s);
//            }
//            br.close();
//            JSONArray json = JSON.parseArray(sbuffer.toString());
//            return success(json);
//        } catch (Exception e) {
//            log.error("getManagementMenu", e);
//        }
//        return new AjaxResult<>();
//    }

}
